package org.android.bbdroid.gui;

import org.android.bbdroid.guiutils.HTMLUtils;
import org.android.bbdroid.webservice.PHPBBClient;
import org.android.bbdroid.webservice.data.Post;
import org.android.bbdroid.webservice.data.Topic;
import org.android.bbdroid.webservice.data.TopicInfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BBDroidTopicAdapterLazy extends BaseAdapter implements
		AbsListView.OnScrollListener {
	boolean busy = false;
	private Context c;
	int currentLast;
	int currentStart = 0;
	private LayoutInflater inflater;
	//TopicInfo info;
	Topic topic;

	public BBDroidTopicAdapterLazy(Context paramContext,
			TopicInfo paramTopicInfo) {
		//this.info = paramTopicInfo;
		this.topic = PHPBBClient.getInstance().getTopic(paramTopicInfo);

		this.currentLast = topic==null? 0 : this.topic.size() - 1;
		this.inflater = LayoutInflater.from(paramContext);
		this.c = paramContext;
	}

	private void fillView(View paramView, String header, String content,
			String info) {
		TextView tvPostHeader = (TextView) paramView
				.findViewById(org.android.bbdroid.R.id.tvPostHeader);
		TextView tvPostContent = (TextView) paramView
				.findViewById(org.android.bbdroid.R.id.tvPostContent);
		TextView tvPostInfo = (TextView) paramView
				.findViewById(org.android.bbdroid.R.id.tvPostInfo);
		tvPostHeader.setText(header);
		tvPostContent.setText(content);
		tvPostInfo.setText(info);
	}

	private void fillView(View paramView, Post p) {
		TextView tvPostHeader = (TextView) paramView
				.findViewById(org.android.bbdroid.R.id.tvPostHeader);
		TextView tvPostContent = (TextView) paramView
				.findViewById(org.android.bbdroid.R.id.tvPostContent);
		TextView tvPostInfo = (TextView) paramView
				.findViewById(org.android.bbdroid.R.id.tvPostInfo);
		tvPostHeader.setText(p.getAuthor());
		tvPostContent.setText(HTMLUtils.fromHTML(p.getPreview()));
		tvPostInfo.setText(c.getString(org.android.bbdroid.R.string.date_text)
				+ ": " + p.getDate());
	}

	public int getCount() {
		return topic==null? 0 : topic.getTopicInfo().getReplies()+1;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public Post getPost(int position) {
		PHPBBClient client = PHPBBClient.getInstance();
		if ((position < this.currentStart) || (position >= this.currentLast)) {
			this.currentStart = position;
			if(topic!=null){
				this.topic = client.getTopic(this.topic.getTopicInfo(), position);
				this.currentLast = topic==null? 0 : this.topic.size() - 1;
			}
		}
		return topic==null? null : topic.get(position - this.currentStart);
	}

	public View getView(int position, View v, ViewGroup vg) {
		if (v == null)
			v = this.inflater.inflate(org.android.bbdroid.R.layout.post, null);
		
		if (position % 2 == 0) 
			v.setBackgroundResource(org.android.bbdroid.R.color.post_background);
		else
			v.setBackgroundResource(org.android.bbdroid.R.color.post_background_lighter);
		
		if (!busy){
			Post p=getPost(position);
			if(p!=null)
				fillView(v, p);
			v.setTag(null);
		}else{
			fillView(v, "", this.c.getString(org.android.bbdroid.R.string.loading_text), "");
			v.setTag(this);
		}
		return v;
	}

	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
            int totalItemCount) {
	}

	public void onScrollStateChanged(AbsListView view, int scrollState){
		switch (scrollState) {
        case OnScrollListener.SCROLL_STATE_IDLE:
        	busy = false;
			int start = view.getFirstVisiblePosition();
			int end = view.getLastVisiblePosition();
			for (int i = 0; i < (end - start) + 1; i++) {
				View v = view.getChildAt(i);
				if (v.getTag() != null){
					fillView(v, getPost(start + i));
					v.setTag(null);
				}
			}
			break;
        case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
        case OnScrollListener.SCROLL_STATE_FLING:
        	this.busy = true;
			break;
		}
	}
}